package client

import (
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/urfave/cli/v2"
	"io/ioutil"
	"net/http"
	"strings"
)

// default userid = "tsb", passwd = "123456"

const cliUrl = "http://39.101.140.145:30000"

var (
	loginUrl string

	clientId            string
	responseType        string
	redirectUri         string
	scope               string
	state               string
	nonce               string
	codeChallengeMethod string
	codeChallenge       string
)

type LoginInfo struct {
	Username     string `json:"username"`
	Password     string `json:"password"`
	Application  string `json:"application"`
	AutoSignin   bool   `json:"autoSignin"`
	Organization string `json:"organization"`
	PhonePrefix  string `json:"phonePrefix"`
	SamlRequest  string `json:"samlRequest"`
	RequestType  string `json:"type"`
}

func init() {
	loginUrl = "http://39.101.140.145:30003/signin/login"

	clientId = "48a37c1ecd28f2818bca"
	responseType = "code"
	redirectUri = "http://39.101.140.145:30003/callback"
	scope = "read"
	state = "gluenet"
}

func casdoorLogin(user string, passwd string) ([]byte, error) {
	//Url, err := url.Parse(loginUrl)
	//if err != nil {
	//	return nil, err
	//}

	//params := url.Values{}
	//params.Set("clientId", clientId)
	//params.Set("responseType", responseType)
	//params.Set("redirectUri", redirectUri)
	//params.Set("scope", scope)
	//params.Set("state", state)
	//params.Set("nonce", nonce)
	//params.Set("code_challenge_method", codeChallengeMethod)
	//params.Set("code_challenge", codeChallenge)
	//
	//Url.RawQuery = params.Encode()
	//urlPath := Url.String()
	//fmt.Println(urlPath)

	//urlValues := make(map[string]string)
	var urlValues []string
	urlValues = append(urlValues, "clientId="+clientId)
	urlValues = append(urlValues, "responseType="+responseType)
	urlValues = append(urlValues, "redirectUri="+redirectUri)
	urlValues = append(urlValues, "scope="+scope)
	urlValues = append(urlValues, "state="+state)
	urlValues = append(urlValues, "nonce="+nonce)
	urlValues = append(urlValues, "code_challenge_method="+codeChallengeMethod)
	urlValues = append(urlValues, "code_challenge="+codeChallenge)

	urlPath := strings.Join(urlValues, "&")
	urlPath = loginUrl + "?" + urlPath

	fmt.Println(urlPath)

	loginInfo := LoginInfo{
		Username:     user,
		Password:     passwd,
		Application:  "gluenet",
		AutoSignin:   false,
		Organization: "gluenet",
		PhonePrefix:  "86",
		SamlRequest:  "",
		RequestType:  "code",
	}

	requestBody, _ := json.Marshal(loginInfo)
	fmt.Println(string(requestBody))

	resp, err := http.Post(urlPath, "application/json", bytes.NewBuffer(requestBody))
	if err != nil {
		return nil, err
	}

	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(resp.StatusCode)

	return body, nil
}

func login(ctx *cli.Context) error {
	user := ctx.String("user")
	passwd := ctx.String("passwd")

	casdoorResponse, err := casdoorLogin(user, passwd)
	if err != nil {
		return err
	}
	fmt.Println(string(casdoorResponse))

	resp, err := http.Get(cliUrl + "/login")
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
	fmt.Println(resp.StatusCode)
	return nil
}
